package com.suluyun.activity;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.suluyun.R;
import com.suluyun.fragment.LoginFragment;
import com.suluyun.fragment.RegFragment;
import com.suluyun.presenter.LoginPresenter;
import com.suluyun.presenter.LoginPresenterCompl;
import com.suluyun.view.LoginView;

public class LoginActivity extends AppCompatActivity{
    private FrameLayout f1;
    private LoginFragment loginFragment;
    private RegFragment regFragment;

    private Button buttonLogin;
    private Button buttonReg;
    private EditText editUser;
    private EditText editPass;
    LoginPresenter loginPresenter;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        bindView();

    }

    private void bindView(){
        //绑定局部帧布局视图
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        loginFragment = LoginFragment.createInstance();
        transaction.add(R.id.login_fragment_container,loginFragment);
        transaction.commit();
        //控件绑定
        View view = View.inflate(getApplicationContext(),R.layout.login_fragment,null);
        //加载其他控件布局准备
        LayoutInflater factory = LayoutInflater.from(LoginActivity.this);
        final View orherView = factory.inflate(R.layout.login_fragment, null);


    }

    public void otherClick(View view){
        switch (view.getId()){
            case R.id.button_login:
                loginPresenter.setProgressBarVisiblity(View.VISIBLE);
                buttonLogin.setEnabled(false);
                buttonReg.setEnabled(false);
                loginPresenter.doLogin(editUser.getText().toString(), editPass.getText().toString());
                break;
            case R.id.button_reg:
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                hideAllFragment(transaction);
                if (regFragment==null){
                    regFragment = RegFragment.createInstance();
                    transaction.add(R.id.login_fragment_container,regFragment);
                }else{
                    transaction.show(regFragment);
                }
                transaction.commit();
                break;
        }

    }

    public void hideAllFragment(FragmentTransaction transaction){
        if(loginFragment!=null){
            transaction.hide(loginFragment);
        }
        if(regFragment!=null){
            transaction.hide(regFragment);
        }
    }

    public void Destory(){
        this.finish();
    }
}
